package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.utils.PMSLog;
import pms.common.value.UserDataValue;
import pms.common.value.UserSessionValue;

public class UserDataDaoImpl implements UserDataDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	public void insert(UserDataValue value) throws ParseException {
		String sql="insert into PMS.TBL_USER (USER_NAME,USER_DISP_NAME,USER_PASSWORD,USER_RIGHTS,USER_COMPANY,USER_GROUP_ID)values(?,?,?,?,?,?)";

		Object[] paras = new Object[]{
				value.getUserName(),
				value.getUserDispName(),				
				value.getPassword(),
				value.getUserRights(),
				value.getUserCompany(),				
				value.getUserGroupId()				
		};
		jdbcTemplate.update(sql, paras);
	};

	public void update(UserDataValue value) throws ParseException {
		String sql="update PMS.TBL_USER set USER_NAME = ?,USER_DISP_NAME = ?,USER_PASSWORD = ?,USER_RIGHTS = ?,USER_COMPANY = ?,USER_GROUP_ID = ? where USER_ID = ?";

		Object[] paras = new Object[]{
				value.getUserName(),
				value.getUserDispName(),				
				value.getPassword(),
				value.getUserRights(),
				value.getUserCompany(),				
				value.getUserGroupId(),
				value.getUserId()
		};
		jdbcTemplate.update(sql, paras);
	};	
	
	public void delete(UserDataValue value) {
		String sql="delete from PMS.TBL_USER where USER_ID = ?";

		Object[] paras = new Object[]{
				value.getUserId()
		};
		jdbcTemplate.update(sql, paras);
	};
	
	public List<UserDataValue> select(int userid, String userRights,int userGroupId) {
		PMSLog.start();
		PMSLog.debug("userid"+userid);
		PMSLog.debug("userRights"+userRights);
		PMSLog.debug("userGroupId"+userGroupId);		
		final List<UserDataValue> result = new ArrayList<UserDataValue>();
		if ("1".equals(userRights.trim())){
			PMSLog.debug("select1");			
			String sql = "SELECT USER_ID,USER_NAME,USER_DISP_NAME,USER_PASSWORD, USER_COMPANY, USER_GROUP_ID, USER_RIGHTS FROM PMS.TBL_USER where USER_GROUP_ID = ? order by USER_RIGHTS";	
					
			jdbcTemplate.query(sql, new Object[]{userGroupId}, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				UserDataValue l_userDataValue = new UserDataValue();
				l_userDataValue.setUserId(rs.getInt("USER_ID"));
				l_userDataValue.setUserName(rs.getString("USER_NAME"));
				l_userDataValue.setUserDispName(rs.getString("USER_DISP_NAME"));
				l_userDataValue.setPassword(rs.getString("USER_PASSWORD"));
				
				l_userDataValue.setUserCompany(rs.getString("USER_COMPANY"));
				l_userDataValue.setUserGroupId(rs.getInt("USER_GROUP_ID"));
				l_userDataValue.setUserRights(rs.getString("USER_RIGHTS"));
				result.add(l_userDataValue);
			}					
			});
		} else {
			PMSLog.debug("select2");	
			PMSLog.debug("userRights"+userRights);			
			String sql = "SELECT USER_ID,USER_NAME,USER_DISP_NAME,USER_PASSWORD, USER_COMPANY, USER_GROUP_ID, USER_RIGHTS FROM PMS.TBL_USER where USER_ID = ?";				
			jdbcTemplate.query(sql, new Object[]{userid}, new RowCallbackHandler(){
				public void processRow(ResultSet rs) throws SQLException {
					UserDataValue l_userDataValue = new UserDataValue();
					l_userDataValue.setUserId(rs.getInt("USER_ID"));
					l_userDataValue.setUserName(rs.getString("USER_NAME"));
					l_userDataValue.setUserDispName(rs.getString("USER_DISP_NAME"));
					l_userDataValue.setPassword(rs.getString("USER_PASSWORD"));
					l_userDataValue.setUserCompany(rs.getString("USER_COMPANY"));
					l_userDataValue.setUserGroupId(rs.getInt("USER_GROUP_ID"));
					l_userDataValue.setUserRights(rs.getString("USER_RIGHTS"));
					result.add(l_userDataValue);
				}					
				});			
		}
		PMSLog.end();			
		if(result!= null)
			return result;
	    else 
			return null;
		
	}	

}
